<?php
/**
 * Description of CommentWidget
 *
 * @author Alexey
 */
class CommentFlowWidget extends CWidget {

    public $commentFlowId;
    public $count;
    public $show = CommentFlowWidget::SHOW_LIST;
    public $comment;
    public $style = CommentFlowWidget::STYLE_COMMENT;

    const SHOW_ITEM = 'item';
    const SHOW_LIST = 'list';

    const STYLE_QUEST_ANSWER = 'quest-answer';
    const STYLE_COMMENT = 'comment';

    private $_itemView;
    private $_listView;
    
    public function init()
    {
        parent::init();
        if ($this->style == CommentFlowWidget::STYLE_COMMENT)
        {
            $this->_itemView = "commentItemView";
            $this->_listView = "commentFlowView";
        }
        else if ($this->style == CommentFlowWidget::STYLE_QUEST_ANSWER)
        {
            $this->_itemView = "commentItemView";
            $this->_listView = "answerFlowView";
        }
        else 
        {
            throw new CException("Unknown comment style.");
        }
    }

    public function run()
    {
        if (Yii::app()->user->isGuest)
        {
            return;
        }
        if ($this->show == CommentFlowWidget::SHOW_LIST)
        {
            $comments = CommentFacade::getLatesComment($this->commentFlowId);
            $newComment = new Comment;
            $this->render($this->_listView,
                           array('comments'      => $comments,
                                 'newComment'    => $newComment,
                                 'commentFlowId' => $this->commentFlowId)
            );
        }
        else if ($this->show == CommentFlowWidget::SHOW_ITEM)
        {
            $this->render($this->_itemView, array('comment' => $this->comment));
        }
    }
}
?>
